package routers

import (
	"gin-base/admin/actions/admin"
	"gin-base/admin/actions/app-release"
	"gin-base/admin/actions/storage"
	app_release2 "gin-base/api/actions/app-release"
	"gin-base/api/actions/train"
	"gin-base/api/actions/user-heart"
	"gin-base/api/actions/weather"
	"gin-base/common/middleware/format"
	"gin-base/common/middleware/header"
	"gin-base/common/middleware/i18n"
	"gin-base/common/middleware/token"
	"net/http"

	"github.com/gin-gonic/gin"
)

// Load loads the router
func Load(middleware ...gin.HandlerFunc) http.Handler {
	e := gin.New()
	e.Use(gin.Recovery(), gin.Logger())

	e.Use(header.Options)
	e.Use(header.Secure)

	e.Use(middleware...)

	// 静态目录
	e.Static("/public", "./public")

	/**
	接口 v1
	*/
	apiApp := e.Group("/v1", token.SetUser(), i18n.SetI18n())
	{
		//天气接口
		apiApp.GET("weather/index", weather.Index)

		//app升级接口
		apiApp.GET("app-releases/latest", app_release2.Latest)

		//训练记录
		trainR := apiApp.Group("train", token.MustUser())
		{
			trainR.GET("", train.Index)
			trainR.POST("", train.Create)
			trainR.GET("/:id", train.View)
		}
		apiApp.GET("train-month-data", train.GetMonthData)

		//心率测试数据
		apiApp.GET("user-heart", user_heart.Last)
		apiApp.POST("user-heart", user_heart.Create)
	}

	/**
	后台上传接口
	*/
	e.POST("/storage/upload", storage.Upload)

	/**
	后台相关接口
	*/
	adminApp := e.Group("/admin", token.SetAdmin(), format.ResponsePack)
	{
		adminR := adminApp.Group("admin")
		{
			adminR.POST("login", admin.Login)
		}

		appReleaseR := adminApp.Group("app-release", token.MustAdmin())
		{
			appReleaseR.GET("", app_release.List)
			appReleaseR.POST("", app_release.Create)
			appReleaseR.DELETE(":id", app_release.Delete)
		}
	}

	return e
}

// type FileHandler interface {
// 	Index(res http.ResponseWriter, data interface{}) error
// 	Login(res http.ResponseWriter, data interface{}) error
// 	Error(res http.ResponseWriter, data interface{}) error
// 	Asset(res http.ResponseWriter, req *http.Request)
// }
